package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.autoflow.service.AutoflowSubService;
import com.zg.autoflow.req.AutoflowSubAddReq;
import com.zg.autoflow.vo.AutoflowSubBatchVo;
import com.zg.autoflow.vo.AutoflowSubUpdateVo;
import com.zg.common.core.A;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepSubDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepSubPersonDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowSubDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepSub;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepSubPerson;
import com.zg.common.core.dao.autoflow.entity.AutoflowSub;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class AutoflowSubServiceImpl implements AutoflowSubService {

    private final AutoflowSubDao autoflowSubDao;
    private final AutoflowStepSubDao autoflowStepSubDao;
    private final AutoflowStepSubPersonDao autoflowStepSubPersonDao;

    void checkAdd(AutoflowSubAddReq param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowSub addOrUpdate(AutoflowSubAddReq param) {
        //校验逻辑
        checkAdd(param);
        //创建对象
        AutoflowSub obj = new AutoflowSub();
        //属性处理
        BeanUtil.copyProperties(param, obj);

        StringBuffer strBuff = new StringBuffer();
        obj.setSearchStr(strBuff.toString());
        if (ObjectUtils.isEmpty(param.getId())){
            autoflowSubDao.save(obj);
        }else {
            autoflowSubDao.updateById(obj);
        }
        autoflowStepSubDao.remove(new LambdaQueryWrapper<AutoflowStepSub>().eq(AutoflowStepSub::getSubFlowId,obj.getId()));
        autoflowStepSubPersonDao.remove(new LambdaQueryWrapper<AutoflowStepSubPerson>().eq(AutoflowStepSubPerson::getSubFlowId,obj.getId()));
        List<AutoflowStepSub> stepSubVos = param.getStepSubVos();
        stepSubVos.forEach(line->{
            line.setSubFlowId(obj.getId());
            line.setFlowCode(obj.getFlowCode());
            autoflowStepSubDao.save(line);
            List<Long> personList = line.getPersonList();
            personList.forEach(person->{
                AutoflowStepSubPerson autoflowStepSubPerson = new AutoflowStepSubPerson();
                autoflowStepSubPerson.setFlowCode(obj.getFlowCode());
                autoflowStepSubPerson.setStepCode(line.getStepNo());
                autoflowStepSubPerson.setSubFlowId(obj.getId());
                autoflowStepSubPerson.setStepHandler(person);
                autoflowStepSubPersonDao.save(autoflowStepSubPerson);
            });
        });
        return obj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestParam("ids") List<Long> idList) {
        autoflowSubDao.removeBatchByIds(idList);
        autoflowStepSubDao.remove(new LambdaQueryWrapper<AutoflowStepSub>().in(AutoflowStepSub::getSubFlowId,idList));
        autoflowStepSubPersonDao.remove(new LambdaQueryWrapper<AutoflowStepSubPerson>().in(AutoflowStepSubPerson::getSubFlowId,idList));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowSub update(AutoflowSubUpdateVo param) {
        //查询对象
        AutoflowSub oldObj = autoflowSubDao.getById(param.getId());
        A.isInDatabase(oldObj, "要更新的对象");
        //属性处理
        BeanUtil.copyProperties(param, oldObj);
        StringBuffer strBuff = new StringBuffer();
        oldObj.setSearchStr(strBuff.toString());
        //更新对象
        autoflowSubDao.updateById(oldObj);
        return oldObj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(AutoflowSubBatchVo param) {
    //创建批量更新列表
    List<AutoflowSub> updateList = new ArrayList<>();
        for (Long id : param.getIdList()) {
            AutoflowSub oldObj = autoflowSubDao.getById(id);
            if(param.getIdCk()){
                oldObj.setId(param.getId());
            }
            if(param.getFieldCk()){
                oldObj.setField(param.getField());
            }
            if(param.getFieldValueCk()){
                oldObj.setFieldValue(param.getFieldValue());
            }
            if(param.getField2Ck()){
                oldObj.setField2(param.getField2());
            }
            if(param.getField2ValueCk()){
                oldObj.setField2Value(param.getField2Value());
            }
            if(param.getNameCk()){
                oldObj.setName(param.getName());
            }
            if(param.getFlowCodeCk()){
                oldObj.setFlowCode(param.getFlowCode());
            }
            oldObj.setSearchStr("");
            updateList.add(oldObj);
        }
        autoflowSubDao.updateBatchById(updateList);
    }
}
